/**
 * Created by 功振 on 2016/4/6.
 */
module.controller('ServiceImageController', function ($scope,$stateParams, $rootScope, $cookies, $state,dockerRegisterService,$filter) {
    var publicImages = [];
    var privateImages = [];

    dockerRegisterService.getImages(true).then(function (d) {
        publicImages = d.data.results;
        publicImages =  setImageColor(publicImages);
    }).catch(function ( err ) {
        alertError( err ) ;
    });

    dockerRegisterService.getImages(false).then(function (d) {
        privateImages = d.data.results;
        privateImages = setImageColor(privateImages);
        $scope.currentImgs = privateImages;
    }).catch(function ( err ) {
        alertError( err ) ;
    });


    $scope.currentImgs = [];
    $scope.$watch('proviteImages',function () {

        // dockerRegisterService.getImages(false).then(function (d) {
        //     $scope.currentPageImgs = d.data.results;
        //   setImageColor()
        //
        //   //Paginator(d.data.results, 10);个人镜像
        // }).catch(function ( err ) {
        //     alertError( err ) ;
        // });
    });

    $scope.clickImage = function (idx) {
        if (idx === 1){
            $scope.currentImgs = publicImages;
        }else if(idx === 0){
            $scope.currentImgs = privateImages;
        }
    };
    $scope.numPerPage = 12;
    $scope.currentPage = 1;
    $scope.currentpageprivate=1;
    function setImageColor (images) {
        var image = images.slice();
        image.forEach(function (imgdata) {
            imgdata.AI = (imgdata.name.substring(0,2)).toUpperCase();
            imgdata.color='#'+getcolor();
        });
        return image;
    }
    $scope.change=function (page) {
        if($scope.currentImgs){
            var begin = ((page - 1) * $scope.numPerPage)
                , end = begin + $scope.numPerPage;
            $scope.currentPageImgs=$scope.currentImgs.slice(begin, end);
        }
    }
  $scope.searchPageHide=false;
  $scope.searchText="";

  $scope.$watch("currentImgs",function () {
        if($scope.currentImgs){
            var begin = (($scope.currentPage - 1) * $scope.numPerPage)
                , end = begin + $scope.numPerPage;
            var images=$scope.currentImgs.slice(begin, end);
            if($scope.searchText == ""){
              $scope.currentPageImgs=$scope.currentImgs.slice(begin, end);
            }else{
              $scope.currentPageImgs = $filter('filter')($scope.currentImgs, {'name': $scope.searchText})
            }
        }

      $scope.searchClick=function () {
          /*       $scope.currentPageServices=utilService.searchObj(currentServices,$scope.services,$scope.searchText,'id')*/

        if ($scope.searchText == "") {
          $scope.searchPageHide=false;

          $scope.currentPageImgs =$scope.currentImgs.slice(begin, end);
        } else {
          $scope.searchPageHide=true;
          $scope.currentPageImgs = $filter('filter')($scope.currentImgs, {'name': $scope.searchText})
        }
      }

    })
    // $scope.$watch("currentImgs+currentpageprivate",function () {
    //     if($scope.currentImgs){
    //         var begin = (($scope.currentpageprivate - 1) * $scope.numPerPage)
    //             , end = begin + $scope.numPerPage;
    //         var images=$scope.currentImgs.slice(begin, end);
    //         $scope.currentPageImgs=$scope.currentImgs.slice(begin, end);
    //     }
    // })
    // $scope.currentPageImgs = [];
    //$scope.currentPage = 1;
    // $scope.$watch("Imgdatas + currentPage", function(){
    //     if ($scope.Imgdatas) {
    //         var begin = (($scope.currentPage - 1) * $scope.numPerPage)
    //             , end = begin + $scope.numPerPage;
    //         var images = $scope.Imgdatas.slice(begin, end);
    //         //var image= $scope.MarketImgdatas;
    //         images.forEach(function (imgdata) {
    //             imgdata.AI = (imgdata.name.substring(0,2)).toUpperCase();
    //             imgdata.color='#'+getcolor();
    //         })
    //         $scope.currentPageImgs =images;
    //     }
    // });

    // $scope.currentPagePrivateImgs = [];
    // $scope.currentPrivatePage = 1;
    var getcolor=function () {
        var colorlist=['49cb6f','01cad7','ef6293','ffc051','bbbbbb'];
        var dex=Math.floor(Math.random()*5);
        return colorlist[dex];
    };
    // $scope.$watch("PrivateImgdatas + currentPrivatePage", function() {
    //     if ($scope.PrivateImgdatas) {
    //         var begin = (($scope.currentPrivatePage - 1) * $scope.numPerPage)
    //             , end = begin + $scope.numPerPage;
    //        // $scope.currentPagePrivateImgs = $scope.PrivateImgdatas.slice(begin, end);
    //         var images = $scope.PrivateImgdatas.slice(begin, end);
    //         //var imgdata=$scope.PrivateImgdatas;
    //         images.forEach(function (imgdata) {
    //             //images[index].AI=(imagedata.name).toUpperCase();
    //             imgdata.AI = (imgdata.name.substring(0,2)).toUpperCase();
    //
    //
    //             imgdata.color='#'+getcolor();
    //         })
    //         $scope.currentPagePrivateImgs=images;
    //     }
    // });
    // $scope.numPerPage = 10;
    //
    // $scope.currentPageImgs = [];
    // $scope.currentMarketPage = 1;
    // $scope.$watch("MarketImgdatas + currentMarketPage", function(){
    //     if ($scope.currentPageImgs) {
    //         var begin = (($scope.currentPageImgs - 1) * $scope.numPerPage)
    //             , end = begin + $scope.numPerPage;
    //         $scope.currentPageImgs = $scope.currentPageImgs.slice(begin, end);
    //     }
    // });

    // $scope.currentPagePrivateImgs = [];
    // $scope.currentPrivatePage = 1;
    // $scope.$watch("PrivateImgdatas + currentPrivatePage", function() {
    //     if ($scope.PrivateImgdatas) {
    //         var begin = (($scope.currentPrivatePage - 1) * $scope.numPerPage)
    //             , end = begin + $scope.numPerPage;
    //         $scope.currentPagePrivateImgs = $scope.PrivateImgdatas.slice(begin, end);
    //     }
    // });

    function alertError( err ) {
        if( !err || !err["data"]){
            err ={
                data:"Request timeout !"
            }
        }
        var message = "[GetImageError]"+err.data ;
        console.log( message )
        alertify.error( message ) ;
    }


});